<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- base href="http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html" --></head><body><div style="background:#fff;border:1px solid #999;margin:-1px -1px 0;padding:0;"><div style="background:#ddd;border:1px solid #999;color:#000;font:13px arial,sans-serif;font-weight:normal;margin:12px;padding:8px;text-align:left">Esta es la versión en caché de <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html" style="text-decoration: underline; color: rgb(0, 0, 204);">http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html</a>
 de Google. Se trata de una captura de pantalla de la página tal como 
esta se mostraba el 19 May 2011 01:32:30 GMT. Es posible que la <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html" style="text-decoration: underline; color: rgb(0, 0, 204);">página</a> haya sufrido modificaciones durante este tiempo. <a href="http://www.google.com/intl/es/help/features_list.html#cached" style="text-decoration: underline; color: rgb(0, 0, 204);">Más información</a><br><br><div style="float:right"><a href="http://webcache.googleusercontent.com/search?q=cache:ZryXJ66yvBEJ:www.codeslinger.co.uk/pages/projects/gameboy/beginning.html+codeslinger+gameboy+getting+started&amp;hl=es&amp;gl=ar&amp;strip=1" style="text-decoration: underline; color: rgb(0, 0, 204);">Versión de solo texto</a></div>
<div>Se han resaltado estos términos de búsqueda: <span style="background:#ffff66;color:black;font-weight:bold">codeslinger</span>&nbsp;<span style="background:#a0ffff;color:black;font-weight:bold">gameboy</span>&nbsp;<span style="background:#99ff99;color:black;font-weight:bold">getting</span>&nbsp;<span style="background:#ff9999;color:black;font-weight:bold">started</span>&nbsp;&nbsp;</div></div></div><div style="position:relative">



<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="_Emulation Programming_">
<meta name="keywords" content="_Emulation,Chip8,Gameboy,NES,SNES,Genesis,Master,System,Mega,Drive_">
<meta name="author" content="CodeSlinger /">
<link rel="stylesheet" type="text/css" href="1%20Getting%20Started_files/css.css" media="screen,projection" title="CodeSlinger (screen)">
<link rel="stylesheet" type="text/css" href="1%20Getting%20Started_files/print.css" media="print">
<title>codeslinger.co.uk</title>



<div>
<br><br>
</div>

<div id="container">
<div id="logo">
<h1><a href="http://www.codeslinger.co.uk/index.html"><b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk</a></h1>
</div>

<div id="navitabs">
<h2 class="hide">Site menu:</h2>
<a class="navitab" href="http://www.codeslinger.co.uk/index.php">Home</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/basics.html">Basics</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/zuko.html">Zuko</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/megadrive.html">Mega Drive/Genesis</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/mastersystem.html">Master System</a><span class="hide"> | </span>
<a class="activenavitab" href="http://www.codeslinger.co.uk/pages/projects/gameboy.html"><b style="color:black;background-color:#a0ffff">Gameboy</b></a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/chip8.html">Chip8</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/blog/index.html">Blog</a><span class="hide"> | </span>
</div>
	
<div id="desc">
<h2><b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk</h2>
<p> <b style="color:black;background-color:#a0ffff">Gameboy</b> - <b style="color:black;background-color:#99ff99">Getting</b> <b style="color:black;background-color:#ff9999">Started</b>.</p>

</div>

<div id="main">

<h3><b>Type definitions:</b> </h3>
<p>
Like all machines the <b style="color:black;background-color:#a0ffff">gameboy</b>
 uses data types of different sizes, mainly bytes (8 bits) and words (16
 bits). The z80-like instruction set also uses signed bytes and signed 
words. So
to make understanding of the code easier I have typedef'd these to give 
the following:

</p><p class="code"> 
typedef unsigned char BYTE ;<br>
typedef char SIGNED_BYTE ;<br>
typedef unsigned short WORD ; <br>
typedef signed short SIGNED_WORD ;<br>
</p>

I highly recommend you do the same as it makes understanding what value 
range a variable can store easier to understand and to track down any 
overflow errors.

<p></p>

<h3><b>Loading In The Game:</b> </h3>
<p>

The maximum size of a <b style="color:black;background-color:#a0ffff">gameboy</b> rom is 0x200000 bytes so if we have an array of this size then this is where we can store the game. This array will represent
the <b style="color:black;background-color:#a0ffff">gameboy</b> cartride.
As you will read in the section called "Memory Control and Map" the internal <b style="color:black;background-color:#a0ffff">gameboy</b> memory only stores the first 0x8000 bytes of the cartride in memory and the rest is left
in the cartridge (the emulator will have to swap data in and out the cartride to memory as needed)<br><br>

Declaration of cartridge memory:

</p><p class="code"> 
BYTE m_CartridgeMemory[0x200000] ;
</p>

Loading a game into cartridge memoy:


<p class="code"> 
memset(m_CartridgeMemory,0,sizeof(m_CartridgeMemory)) ;<br><br>

FILE *in;<br>
in = fopen( "Tetris.gb", "rb" );<br>
fread(m_CartridgeMemory, 1, 0x200000, in);<br>
fclose(in);
</p>


<p></p>

<h3><b>The Emulation Loop:</b> </h3>
<p>
Unlike the chip8 the <b style="color:black;background-color:#a0ffff">gameboy</b> timing is well documented and it is crucial that the speed of the <b style="color:black;background-color:#a0ffff">gameboy</b> is emulated correctly. The <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/files/GB.pdf"> <b style="color:black;background-color:#a0ffff">gameboy</b> document</a>
that I'm hosting gives the timing in clock cycles for each opcode. So if
 we know how many clock cycles each instruction takes on the original <b style="color:black;background-color:#a0ffff">gameboy</b>
 hardware we can
add this to a running counter to keep track of timing. One of the main 
benefits in keeping a track of how long each instruction takes is we can
 synchronize this with the
graphic emulation and the <b style="color:black;background-color:#a0ffff">gameboy</b> clock timers to emulate these at the same rate as the cpu.<br><br>

I use SDL for my graphic emulation and this comes with functionality to 
limit the frame rate of the emulator. I have chosen to limit the frame 
rate to 60 frames per second, the
reason for this is the <b style="color:black;background-color:#a0ffff">gameboy</b> refreshes the screen 60 times a second. According to game <a href="http://nocash.emubase.de/pandocs.htm">pan docs site</a> the amount of clock cycles
the <b style="color:black;background-color:#a0ffff">gameboy</b> can 
exectue every second is 4194304 which means that if each frame we update
 the emulator 60 times a second the each frame will execute 
69905(4194304/60) clock cycles. This will
ensure the emulator is run at the correct speed.

</p><p class="code"> 

void Emulator::Update( )<br>
{<br>
&nbsp; const int MAXCYCLES = 69905 ;<br>
&nbsp; int cyclesThisUpdate = 0 ; <br><br>

&nbsp; while (cyclesThisUpdate &lt; MAXCYCLES)<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; int cycles = ExecuteNextOpcode( ) ; <br>
&nbsp;&nbsp;&nbsp;&nbsp; cyclesThisUpdate+=cycles ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; UpdateTimers(cycles) ; <br>
&nbsp;&nbsp;&nbsp;&nbsp; UpdateGraphics(cycles) ; <br>
&nbsp;&nbsp;&nbsp;&nbsp; DoInterupts( ) ;<br>
&nbsp; }<br>

RenderScreen( ) ;<br>
}
</p>

So if the above function is called 60 times a second then RenderScreen 
is also called 60 times a second which is the same as the <b style="color:black;background-color:#a0ffff">gameboy</b>.
 The while loop
ensures that the correct amount of instructions is being executed this 
frame (assuming of course that the function ExecuteNextOpcode is 
returning the
correct clock cycles the next opcode took to execute). Also the timers 
and graphics are being passed how many clock cycles the opcode took so 
they can update
at the same rate as the cpu. After every instruction the interupts need 
to be checked and if needed then processed.
<br><br>
<p></p>



</div>

<div id="sidebar">
<h3><b style="color:black;background-color:#a0ffff">Gameboy</b> Emulation:</h3>

<p>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html"><b style="color:black;background-color:#99ff99">Getting</b> <b style="color:black;background-color:#ff9999">Started</b></a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/hardware.html">The Hardware</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/memory.html">Memory Control and Map</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/banking.html">ROM and RAM Banking</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/timers.html">The Timers</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/interupts.html">Interupts</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/lcd.html">LCD</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/dma.html">DMA Transfer</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/graphics.html">Graphic Emulation</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/joypad.html">Joypad Emulation</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html">Opcode Examples</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/finished.html">Finished Product</a><span class="hide"> | </span>

</p>

</div>
    
<div id="footer">
Copyright © 2008 <b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk
</div>

</div>




</div></body></html>